package osirix.ce;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import osirix.ce.enumerados.EAction;

public class UtilWs {
	
	static HttpParams httpParams = new BasicHttpParams();
	
	private static final int TIMEOUT_MILLISEC = 100;
	
	/*
	 * Metodo encargado de conectarse con el WebService para realizar todas las solicitudes con los servicios
	 */
	public static void getWebService(HashMap<String, String> listaParametros, String action) {
		
		HttpConnectionParams.setConnectionTimeout(httpParams,
				TIMEOUT_MILLISEC);
		HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
		
		Iterator itera = listaParametros.entrySet().iterator();
		
		while (itera.hasNext()) {
			Map.Entry e = (Map.Entry) itera.next();
			httpParams.setParameter(e.getKey().toString(), e.getValue().toString());			
		}
		
		HttpClient httpClient = new DefaultHttpClient(httpParams);
		
		String url = "http://192.168.0.127:8084/CEWeb/resources/ws/" + action;
		
		HttpPost httpPost = new HttpPost(url);
		
		HttpResponse response;
		
		try {
			response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			
			if (entity != null) {
				InputStream instream = entity.getContent();
				String resultado = Util.convertStreamToString(instream);
				
				JSONObject json = new JSONObject(resultado);
				
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//retornar el JSON
		
	}

}
